package org.wlldTest.lesson14;

import org.dromara.easyai.matrixTools.Matrix;
import org.dromara.easyai.tools.LVQ;
import org.dromara.easyai.tools.MatrixBody;

import java.util.Random;

public class Test {
    public static void main(String[] args) throws Exception {
        test();
    }

    public static Matrix getMatrix(float nub) throws Exception {
        Random random = new Random();
        Matrix matrix = new Matrix(1, 4);
        float n = random.nextFloat() / 10 + nub;//[nub,nub+0.1]
        for (int j = 0; j < 4; j++) {
            matrix.setNub(0, j, n);
        }
        return matrix;
    }

    public static void test() throws Exception {
        LVQ lvq = new LVQ(2, 100, 0.01f);
        for (int i = 0; i < 50; i++) {//五十组假数据
            MatrixBody matrixBody1 = new MatrixBody();
            matrixBody1.setId(0);
            matrixBody1.setMatrix(getMatrix(-0.1f));
            MatrixBody matrixBody2 = new MatrixBody();
            matrixBody2.setId(1);
            matrixBody2.setMatrix(getMatrix(0.1f));
            lvq.insertMatrixBody(matrixBody1);lvq.insertMatrixBody(matrixBody1);
            lvq.insertMatrixBody(matrixBody2);
        }
        lvq.start();
        MatrixBody[] model = lvq.getModel();//返回每个簇的均衡坐标
        Matrix matrix1 = getMatrix(-0.1f);
        Matrix matrix2 = getMatrix(0.1f);
        int type0 = getType(model, matrix1, lvq);
        int type1 = getType(model, matrix2, lvq);
        System.out.println("type0:" + type0 + ",type1:" + type1);
    }

    public static int getType(MatrixBody[] model, Matrix myMatrix, LVQ lvq) throws Exception {
        float minDist = 100;
        int id = -1;
        for (MatrixBody matrixBody : model) {
            Matrix matrix = matrixBody.getMatrix();//每个簇的均衡点坐标
            float dist = lvq.vectorEqual(myMatrix, matrix);
            if (dist < minDist) {
                minDist = dist;
                id = matrixBody.getId();
            }
        }
        return id;
    }
}
